home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS15.ADF / C / Stat.C < prev    next >
C/C++ Source or Header  |  1988-04-20  |  4KB  |  138 lines

  1. /* stat.c An enhanced version of the AmigaDOS command STATUS.
  2.  *
  3.  * Copyright 1986 by James M Synge. Sept. 2, 1986.
  4.  * Permission is granted for unlimited non-commercial use.
  5.  *
  6.  * Compiled using Aztec C, but it shouldn't be that hard to change
  7.  * for Lattice C
  8.  *
  9.  * The makefile file is very short:
  10.  *------------------------------
  11.  *  stat: stat.o
  12.  *     ln -v stat.o -lc
  13.  *------------------------------
  14.  * UUCP: uw-beaver!geops!uw-atm!james
  15.  */
  16.  
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <exec/tasks.h>
  20.  
  21. #include <libraries/dosextens.h>
  22.  
  23. char buf1[32], buf2[32];
  24.  
  25. /* DOSBase is already set up (by _main?) */
  26. extern struct DosLibrary *DOSBase;
  27.  
  28. /* Need a macro to translate a BPTR to an APTR */
  29. #ifdef BADDR
  30. #undef BADDR
  31. #endif
  32. #define BADDR(bptr) (((long)bptr) << 2)
  33.  
  34. /* By experimentation I determined that the command line arguments are */
  35. /* located at (tc_SPLower + 0x280).  This may change in the future. */
  36. /* If STAT starts printing garbage for the args, just comment out this: */
  37. #define ARGS_OFFSET 0x280
  38.  
  39. main(argc, argv)
  40. int argc;
  41. char *argv[];
  42. {
  43.        int i, tasknum, pri, strncmp(); char *msg, *index();
  44.        long process, processes, *ta;
  45.        struct Process *pr;
  46.        struct RootNode *rn;
  47.        struct CommandLineInterface *cli;
  48.  
  49.        /* Find the RootNode */
  50.        rn = (struct RootNode *)(DOSBase->dl_Root);
  51.  
  52.        /* ta points to the array of tasks. The first location is the */
  53.        /* maximum number of processes, followed by that many pointers. */
  54.        /* Each pointer points to the MsgPort within the Process data */
  55.        /* structure of each AmigaDos process, otherwise its value is 0L. */
  56.        /* The AmigaDOS Technical Reference Manual calls these pointers */
  57.        /* process ids. */
  58.  
  59.        ta = (long *) BADDR(rn->rn_TaskArray);
  60.  
  61.        /* How many AmigaDOS processes are there? */
  62.  
  63.        processes = *ta++;
  64.  
  65.        /* Print the title line. */
  66.        printf("Task Pri  Address Command\t\t\t   Directory\n");
  67.  
  68.        /* Loop through each live process, printing info. */
  69.  
  70.        for (process = 0; process < processes; process++ ) {
  71.  
  72.                /* Get the next process id (i.e. struct MsgPort *) */
  73.                msg = (char *)(*ta++);
  74.                if (!msg) continue; /* No associated Process? */
  75.  
  76.                /* Pointer to struct Process */
  77.                pr = (struct Process *)(msg - sizeof(struct Task));
  78.  
  79.                /* CLIs have small positive task numbers */
  80.                tasknum = (int)(pr->pr_TaskNum);
  81.  
  82.                /* The priority of the task is in the Node structure */
  83.                pri = (int)(pr->pr_Task.tc_Node.ln_Pri);
  84.  
  85.                cli = (struct CommandLineInterface *) BADDR(pr->pr_CLI);
  86.  
  87.                if (cli) {
  88.                    moveBSTR(cli->cli_CommandName, buf1, 32);
  89.                    if (!buf1[0]) strcpy(buf1, "Waiting for a command");
  90. #ifdef ARGS_OFFSET
  91.                    else {
  92.                        msg = (char *)(pr->pr_Task.tc_SPLower) + ARGS_OFFSET;
  93.                        if (*msg && *msg != '\n') {
  94.                            strncat(buf1, " ", 32);
  95.                            strncat(buf1, msg, 32);
  96.                            msg = index(buf1, '\n');  /* Look for a \n */
  97.                            if (msg) *msg = '\0';
  98.                        }
  99.                    }
  100. #endif
  101.                    moveBSTR(cli->cli_SetName, buf2, 32);
  102.                } else {
  103.                        strcpy(buf1, "Not a CLI");
  104.                        buf2[0] = 0;
  105.                }
  106.  
  107.                printf("%3d %4d %8lx %-32s %s\n",
  108.                        tasknum, pri, pr, buf1, buf2);
  109.        }
  110.        exit(0);
  111. }
  112.  
  113. /* moveBSTR copies a BSTR to a C char string. */
  114. moveBSTR(bptr, buffer, maxlen)
  115. BSTR bptr;
  116. char *buffer;
  117. int maxlen; /* size of buffer[] */
  118. {
  119.        register char *ptr;
  120.        register unsigned int len, i;
  121.        unsigned char l;
  122.  
  123.        ptr = (char *) BADDR(bptr); /* Make a char* to the length */
  124.  
  125.        l = (unsigned int) (*ptr++); /* Get the length and increment ptr */
  126.  
  127.        if (!(len = l)) {
  128.                *buffer = '\0'; /* Mark the end of the string. */
  129.                return;
  130.        }
  131.        if (len > maxlen) len = maxlen;
  132.        for(i = 0; i < len; i++) *buffer++ = *ptr++; /* Copy it. */
  133.  
  134.        if (i < maxlen) *buffer = '\0'; /* If there is room, mark the end */
  135.        return;
  136. }
  137.  
  138.